#include "inputdialog.h"
#include "ui_inputdialog.h"
#include <QDesktopWidget>

InputDialog::InputDialog(const QString &label, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InputDialog)
{
    ui->setupUi(this);
    if(label.length()>0)
        ui->label->setText(label);
    else
        ui->label->hide();

#ifdef Q_OS_ANDROID
    setWindowFlags(Qt::ToolTip);
    if(parent) {
        QRect rect = parent->geometry();
        rect.setHeight(60);
        setGeometry(rect);
    } else {
        QRect rect = QApplication::desktop()->screenGeometry();
        rect.setHeight(60);
        setGeometry(rect);
    }
    setStyleSheet("QDialog{ background-color: rgb(235, 242, 255);border: 1px solid black; border-style: solid; border-radius: 4px; }");
#endif

    connect(ui->button_0, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_1, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_2, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_3, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_4, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_5, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_6, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_7, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_8, SIGNAL(clicked()), this, SLOT(digitClicked()));
    connect(ui->button_9, SIGNAL(clicked()), this, SLOT(digitClicked()));
}

InputDialog::~InputDialog()
{
    delete ui;
}

void InputDialog::done(int res)
{
    if(res == QDialog::Accepted) {
        QString s = ui->editValue->text();
        if( s.at(0) == '.' && s.length() > 1 )
            s = "0" + s;
        else if( s.at(0) == '-' && s.length() == 1 )
            s = "";
        else if( s.at(0) == '.' && s.length() == 1 )
            s = "";
        ui->editValue->setText(s);
    }
    return QDialog::done(res);
}

QVariant InputDialog::input(const QString &initValue)
{
    ui->editValue->setText(initValue);
    if( this->exec() == QDialog::Accepted )
        return ui->editValue->text();
    return QVariant();
}

void InputDialog::digitClicked()
{
    QString digit = QObject::sender()->objectName().remove("button_");
    QString curValue = ui->editValue->text();
    curValue.append(digit);
    ui->editValue->setText(curValue);
}

void InputDialog::on_button_dot_clicked()
{
    QString curValue = ui->editValue->text();
    if(curValue.indexOf('.')==-1)
        curValue.append('.');
    ui->editValue->setText(curValue);
}

void InputDialog::on_button_plmn_clicked()
{
    QString curValue = ui->editValue->text();
    if(curValue[0] == '-')
        curValue.remove(0,1);
    else
        curValue = "-" + curValue;
    ui->editValue->setText(curValue);
}

void InputDialog::on_button_back_clicked()
{
    QString curValue = ui->editValue->text();
    curValue.remove(curValue.length()-1,1);
    ui->editValue->setText(curValue);
}

void InputDialog::on_button_clear_clicked()
{
    ui->editValue->setText("");
}
